<template>
  <div>
    <div class="regTop">
      <van-nav-bar title="登录" left-arrow @click-left="$router.back()" />
    </div>
    <div class="reg-main">
      <h2 style="margin-left:16px;">欢迎使用莱头条</h2>
      <!-- <van-cell-group> -->
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="username"
         
          placeholder="请输入账号"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />

        <van-field
          v-model="password"
          name="password"
          
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请输入密码' }]"
        />

        <div style="margin: 16px;margin-top:30px">
          <van-button
            round
            block
            type="info"
            color="#813c85"
            native-type="submit"
            >登录</van-button
          >
        </div>
        <div class="footer">
          <div class="footer-l">
            <span @click="$router.push('reg1')">点击注册</span>
          </div>

          <div class="footer-r">
            <span @click="$router.push('orgetPassword')">忘记密码?</span>
          </div>
        </div>
      </van-form>
      <!-- </van-cell-group> -->
    </div>
  </div>
</template>

<script>
import { NavBar, Field, Button, Form } from "vant";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  components: {
    [NavBar.name]: NavBar,
    [Field.name]: Field,
    [Button.name]: Button,
    [Form.name]: Form,
  },
  created() {
    // this.getReg()
  },
  methods: {
    onSubmit(values) {
      let username = this.username;
      let password = this.password;

      console.log(typeof username);
      console.log("submit", values);

      this.$http
        .post("/user/login", {
          username,
          password,
        })
        .then((res) => {
          if (res.code == 0) {
            this.$toast("登录成功");
            let { token, tokenExpired, uid, userInfo } = res;

            localStorage.setItem("token", token);
            localStorage.setItem("userInfo", JSON.stringify(userInfo));
            localStorage.setItem("uid", uid);
            localStorage.setItem("tokenExpired", tokenExpired);

            this.$store.dispatch("getuserInfo", token);
            //  存到vuex中
            // this.$store.commit('save',['uid',uid])
            // this.$store.commit('save',['token',token])
            // this.$store.commit('save',['userInfo',userInfo])
            // this.$store.commit('save',['isLogin',true])

            setTimeout(() => {
              this.$router.back();
            }, 1000);
          } else {
            this.$toast(res.msg || "登陆失败");
          }
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.reg-main{
  margin-top: 50px;
}
.footer {
  padding: 0 16px;
  display: flex;
  justify-content: space-between;
  .footer-l{
    text-align: center;
    width: 50%;
    border-right: 1px solid #ccc;
  }
  .footer-r{
    text-align: center;
    width: 50%;
  }
  em {
    font-style: normal;
    color: cornflowerblue;
  }
}
</style>